import { BehaviorSubject, forkJoin, switchMap } from "rxjs";

const fakeRequest = (n: number): Promise<number> =>
  new Promise<number>((resolve) => {
    setTimeout(() => resolve(n * 2), 1000);
  });

const source = new BehaviorSubject(1);

const obs = source.pipe(
  switchMap((n) => fakeRequest(n)),
);

const sub = forkJoin([obs]).subscribe(console.log);

// setTimeout(() => {
//   console.log("Terminating source...");
//   source.complete();
//   console.log("Source teminated...");
// }, 2000);
//
setTimeout(() => console.log(sub.closed), 3000);
